#!/usr/bin/env python
# coding: utf-8

# In[3]:


from surprise import Dataset
from surprise import Reader
from surprise import BaselineOnly
from surprise import accuracy
from surprise.model_selection import KFold


# In[ ]:


import pandas as pd


# In[18]:


stm = pd.read_csv(r"D:\Desk\steam.csv")
stm=stm.iloc[:,[0,1,4]]
stm.to_csv(r"D:\Desk\data.csv")


# In[19]:


# 数据读取
reader = Reader(line_format='user item rating timestamp', sep=',', skip_lines=1)
data = Dataset.load_from_file('D:Desk\data.csv', reader=reader)


# In[ ]:





# In[20]:


# ALS优化
bsl_options = {'method': 'als','n_epochs': 5,'reg_u': 12,'reg_i': 5}


# In[21]:


# SGD优化
#bsl_options = {'method': 'sgd','n_epochs': 5}
algo = BaselineOnly(bsl_options=bsl_options)
#algo = BaselineOnly()
#algo = NormalPredictor()


# In[22]:


# 定义K折交叉验证迭代器，K=3
kf = KFold(n_splits=3)
for trainset, testset in kf.split(data):
    # 训练并预测
    algo.fit(trainset)
    predictions = algo.test(testset)
    # 计算RMSE
    accuracy.rmse(predictions, verbose=True)


# In[26]:


uid = str(3)
gid = str(2)
# 输出uid对gid的预测结果
pred = algo.predict(uid, gid, r_ui=4, verbose=True)


# In[ ]:




